<template>
  <div>
    <!-- <el-button type="primary" @click="clickHandle">{{ text }}</el-button> -->
    <!-- <c-table-box index checkbox :other-filters="otherFilters" :table-config="tableConfig" /> -->
    <c-table-box
      ref="proTaskTable"
      show-index
      checkbox
      hide-func
      :selected-arry.sync="tableSelected"
      :table-config="tableConfig"
      :other-filters="otherFilters"
      :other-raw="otherRaw"
      @selection-change="selectionChangeHandle"
    >
      <template #btn>
        <c-permission v-if="type==='all'" code="all">
          <c-button type="primary" opt-code="export" @click="exportVisible=true">导出</c-button>
          <c-button type="primary" opt-code="termination" @click="handleStopData">终止数据</c-button>

        </c-permission>
        <c-permission v-if="type==='configuration'" code="configuration">
          <c-button type="primary" opt-code="configuration" @click="handleEdit">产品配置</c-button>
        </c-permission>
        <c-permission v-if="type==='audit'" code="audit">
          <c-button type="primary" opt-code="audit" @click="handleAudit">配置清单审核</c-button>
        </c-permission>
      </template>
    </c-table-box>
    <div>
      <export-files v-if="exportVisible" title="导出" :config="exportAllConfig" :visible.sync="exportVisible" @success="handleExportSuccess" />
    </div>
  </div>
</template>
<script>
import { stopData } from '@/api/proConfig'

export default {
  name: 'List',
  props: {
    type: {
      type: String,
      default: ''
    },
    activeType: {
      type: String,
      default: ''
    },
    serachFilters: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      exportVisible: false,
      activedData: {},
      typeFilters: [],
      tableSelected: [],
      otherRaw: {},
      otherFilter: [{ 'id': 'proConfigType', 'property': 'proConfigType', 'operator': '=', 'value': 'MATCHED_FINISHED' },
        { 'id': 'isValid', 'property': 'isValid', 'operator': '=', 'value': 1 }],
      tableConfig: {
        interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/productConfigSheet/page`,
        hideSearchBar: true,
        sort: 'createTime',
        order: 'DESC',
        apiHeader: { 'Permission-Percode': 'environment', 'Permission-App': 'slm' },
        setting: [
          {
            prop: 'configCode',
            label: '产品配置单号',
            className: 'btn-green',
            isButton: true,
            clickFun: this.toDetail,
            minWidth: 140
          },
          {
            prop: 'configTaskCode',
            label: '产品配置任务',
            minWidth: 140
          },
          {
            prop: 'source',
            label: '任务来源',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'PRODUCT_CONFIG_TASK_SOURCE'
            },
            minWidth: 100
          },
          {
            prop: 'proConfigType',
            label: '产品配置类型',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'PRO_CONFIG_TYPE'
            },
            minWidth: 120
          },
          {
            prop: 'proConfiguratorName',
            label: '产品配置人员',
            minWidth: 140
          },
          {
            prop: 'schedule',
            label: '产品配置进度',
            propEnum: {
              domainCode: 'marketing',
              appCode: 'slm',
              dictTypeCode: 'PRO_CONFIG_SCHEDULE'
            },
            minWidth: 120
          },
          {
            prop: 'constCode',
            label: '门店建设项目号',
            minWidth: 160
          },
          {
            prop: 'shopName',
            label: '所属门店',
            minWidth: 100
          },
          {
            prop: 'clientName',
            label: '所属客户',
            minWidth: 100
          },
          {
            prop: 'officeName',
            label: '所属办事处',
            minWidth: 100
          }
        ]
      }
    }
  },
  computed: {
    // text() {
    //   switch (this.type) {
    //     case 'all':
    //       return '导出'
    //     case 'waitForDistribution':
    //       return '产品配置'
    //     case 'pending':
    //       return '配置清单审核'
    //     default:
    //       return ''
    //   }
    // },
    otherFilters() {
      return [...this.serachFilters, ...this.typeFilters]
    },
    exportAllConfig() {
      return [{
        code: 'export',
        name: '环境成品类样品配置',
        dataApi: '/slm-admin/api/v1/productConfigSheet/exportEnvironment?type=0',
        selectedRows: this.tableSelected,
        paramsRaw: {},
        filtersRaw: this.otherFilters,
        queryField: 'id'
      }]
    }
  },
  watch: {
    type: {
      handler(v) {
        this.setOtherFilters(v)
        // this.setAllTable()
      },
      immediate: true
    },
    serachFilters: {
      handler(v) {
        if (this.activeType !== this.type) return
        this.setOtherFilters(this.type)
        this.$nextTick(() => {
          this.$refs.proTaskTable.initTableData()
        })
      }
    }
  },
  created() {
  },
  activated() {
    if (this.ready) {
      this.$nextTick(() => {
        this.$refs.proTaskTable.initTableData()
      })
    }
  },
  deactivated() {
    this.ready = true
  },
  methods: {
    selectionChangeHandle(rows) {
      this.tableSelected = rows
    },
    handleStopData() {
      if (!this.tableSelected.length) {
        this.$message.warning('请选择【终止数据】项')
        return
      }
      this.$prompt('请输入终止原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^.+$/,
        inputErrorMessage: '请填写终止原因'
      }).then(({ value }) => {
        const form = {
          endReason: value,
          ids: this.tableSelected.map(v => v.id),
          proConfigType: this.tableSelected[0].proConfigType
        }
        stopData(form).then(res => {
          if (res.success) {
            this.$message.success('操作成功')
          }
          this.$nextTick(() => {
            this.$refs.proTaskTable.initTableData('onPage')
          })
        })
      }).catch(() => {

      })
    },
    // 设置过滤条件
    setOtherFilters(type) {
      switch (type) {
        case 'all':
          this.typeFilters = [...this.otherFilter]
          break
        case 'configuration':
          { const arr1 = [{ 'id': 'schedule1', 'property': 'schedule', 'operator': '=', 'value': 'A' }]
            // const arr2 = this.serachFilters.filter(item => item.property !== 'schedule')
            this.typeFilters = [...this.otherFilter, ...arr1]
            this.otherRaw = { 'isEnd': 0 }
          }
          break
        case 'audit':
          { const arr1 = [{ 'id': 'schedule1', 'property': 'schedule', 'operator': '=', 'value': 'B' }]
            // const arr2 = this.serachFilters.filter(item => item.property !== 'schedule')
            this.typeFilters = [...this.otherFilter, ...arr1]
            this.otherRaw = { 'isEnd': 0 }
          }
          break
      }
    },
    // 导出
    handleExport() {
      this.exportVisible = true
    },
    // 产品配置
    handleEdit() {
      if (!this.tableSelected.length) {
        this.$message.warning('请选择【产品配置】项')
        return
      }
      if (this.tableSelected.length > 1) {
        this.$message.warning('该操作仅支持单选')
        return
      }
      this.$router.push({ name: 'PCEnvironmentConfigEdit', params: this.tableSelected[0] })
    },
    // 审核
    handleAudit() {
      if (!this.tableSelected.length) {
        this.$message.warning('请选择【审核】项')
        return
      }
      if (this.tableSelected.length > 1) {
        this.$message.warning('该操作仅支持单选')
        return
      }
      this.$router.push({ name: 'PCEnvironmentAudit', params: this.tableSelected[0] })
    },
    toDetail(row) {
      this.$router.push({ name: 'PCEnvironmentDetail', params: row })
    }
  }
}
</script>
